/**
 * Unicorn Admin Template
 * Version 2.2.0
 * Diablo9983 -> diablo9983@gmail.com
**/
$(function(){
	// UI Dialog
     $("#dialog").dialog({
		autoOpen: false,
		resizeable: false,
		buttons: {
			"Ok": function () {
				$(this).dialog("close");
			},
			"Cancel": function () {
				$(this).dialog("close");
			}
		},
		show: {
			effect: "puff",
			duration: 500
		},
		hide: {
			effect: "puff",
			duration: 500
		}
	});

     // Modal Dialog message
	$("#modal-dialog").dialog({
		autoOpen: false,
		modal: true,
		resizeable: false,
		buttons: {
			Ok: function () {
				$(this).dialog("close");
			}
		},
		show: {
			effect: "drop",
			duration: 400,
			direction: "up"
		},
		hide: {
			effect: "drop",
			duration: 400,
			direction: "down"
		}
	});
	$("#open-dialog").click(function(){
		$("#dialog").dialog("open");
		return false;
	});
	$("#open-modal").click(function(){
		$("#modal-dialog").dialog("open");
		return false;
	})

    $.datepicker.regional['ko'] = {
        closeText: '닫기',
        prevText: '이전달',
        nextText: '다음달',
        currentText: '오늘',
        monthNames: ['1월','2월','3월','4월','5월','6월',
            '7월','8월','9월','10월','11월','12월'],
        monthNamesShort: ['1월','2월','3월','4월','5월','6월',
            '7월','8월','9월','10월','11월','12월'],
        dayNames: ['일','월','화','수','목','금','토'],
        dayNamesShort: ['일','월','화','수','목','금','토'],
        dayNamesMin: ['일','월','화','수','목','금','토'],
        weekHeader: 'Wk',
        dateFormat: 'yy-mm-dd',
        firstDay: 0,
        isRTL: false,
        duration:200,
        showAnim:'show',
        showMonthAfterYear: true,
        yearSuffix: '년'};

    $.datepicker.setDefaults($.datepicker.regional['ko']);
	// Datepicker
	$('#orderDateFrom').datepicker({

        numberOfMonths: 1,
        dateFormat : "yy-mm-dd",
		onClose: function( selectedDate ) {
			$( "#orderDateTo" ).datepicker( "option", "minDate", selectedDate );
		}
	});
	$('#orderDateTo').datepicker({
        defaultDate: "+1w",
		numberOfMonths: 1,
        dateFormat : "yy-mm-dd",
		onClose: function( selectedDate ) {
			$( "#orderDateFrom" ).datepicker( "option", "maxDate", selectedDate );
		}
	});

    $('#regDateFrom').datepicker({

        numberOfMonths: 1,
        dateFormat : "yy-mm-dd",
        onClose: function( selectedDate ) {
            $( "#regDateTo" ).datepicker( "option", "minDate", selectedDate );
        }
    });
    $('#regDateTo').datepicker({
        defaultDate: "+1w",
        numberOfMonths: 1,
        dateFormat : "yy-mm-dd",
        onClose: function( selectedDate ) {
            $( "#regDateFrom" ).datepicker( "option", "maxDate", selectedDate );
        }
    });


    $('#yesterdayComplete-btn').click(function() {
        $('#yesterdayComplete').val('true');
        $('#yesterdayStart').val('false');
        $('#todayComplete').val('false');
        $('#todayStart').val('false');

        $('#searchCondition').submit();

    });

    $('#yesterdayStart-btn').click(function() {
        $('#yesterdayComplete').val('false');
        $('#yesterdayStart').val('true');
        $('#todayComplete').val('false');
        $('#todayStart').val('false');

        $('#searchCondition').submit();

    });

    $('#todayComplete-btn').click(function() {
        $('#yesterdayComplete').val('false');
        $('#yesterdayStart').val('false');
        $('#todayComplete').val('true');
        $('#todayStart').val('false');

        $('#searchCondition').submit();

    });

    $('#todayStart-btn').click(function() {
        $('#yesterdayComplete').val('false');
        $('#yesterdayStart').val('false');
        $('#todayComplete').val('false');
        $('#todayStart').val('true');

        $('#searchCondition').submit();

    });


    $('#orderDaySend-btn').click(function() {
        $('#orderDaySend').val('true');
        $('#orderDayPlus1Send').val('false');
        $('#orderDayPlus2Send').val('false');
        $('#orderDayPlus3Send').val('false');
        $('#doNotSend').val('false');

        $('#searchCondition').submit();
    });

    $('#orderDayPlus1Send-btn').click(function() {
        $('#orderDaySend').val('false');
        $('#orderDayPlus1Send').val('true');
        $('#orderDayPlus2Send').val('false');
        $('#orderDayPlus3Send').val('false');
        $('#doNotSend').val('false');

        $('#searchCondition').submit();
    });

    $('#orderDayPlus2Send-btn').click(function() {
        $('#orderDaySend').val('false');
        $('#orderDayPlus1Send').val('false');
        $('#orderDayPlus2Send').val('true');
        $('#orderDayPlus3Send').val('false');
        $('#doNotSend').val('false');

        $('#searchCondition').submit();
    });

    $('#orderDayPlus3Send-btn').click(function() {
        $('#orderDaySend').val('false');
        $('#orderDayPlus1Send').val('false');
        $('#orderDayPlus2Send').val('false');
        $('#orderDayPlus3Send').val('true');
        $('#doNotSend').val('false');

        $('#searchCondition').submit();
    });

    $('#doNotSend-btn').click(function() {
        $('#orderDaySend').val('false');
        $('#orderDayPlus1Send').val('false');
        $('#orderDayPlus2Send').val('false');
        $('#orderDayPlus3Send').val('false');
        $('#doNotSend').val('true');

        $('#searchCondition').submit();
    });


    $('#gapDayOfStartAndCompletePlus1-btn').click(function() {
        $('#gapDayOfStartAndCompletePlus1').val('true');
        $('#gapDayOfStartAndCompletePlus2').val('false');
        $('#gapDayOfStartAndCompletePlus3').val('false');
        $('#gapDayOfStartAndCompletePlus4').val('false');
        $('#gapDayOfStartAndCompletePlus5').val('false');

        $('#searchCondition').submit();
    });

    $('#gapDayOfStartAndCompletePlus2-btn').click(function() {
        $('#gapDayOfStartAndCompletePlus1').val('false');
        $('#gapDayOfStartAndCompletePlus2').val('true');
        $('#gapDayOfStartAndCompletePlus3').val('false');
        $('#gapDayOfStartAndCompletePlus4').val('false');
        $('#gapDayOfStartAndCompletePlus5').val('false');

        $('#searchCondition').submit();
    });

    $('#gapDayOfStartAndCompletePlus3-btn').click(function() {
        $('#gapDayOfStartAndCompletePlus1').val('false');
        $('#gapDayOfStartAndCompletePlus2').val('false');
        $('#gapDayOfStartAndCompletePlus3').val('true');
        $('#gapDayOfStartAndCompletePlus4').val('false');
        $('#gapDayOfStartAndCompletePlus5').val('false');

        $('#searchCondition').submit();
    });

    $('#gapDayOfStartAndCompletePlus4-btn').click(function() {
        $('#gapDayOfStartAndCompletePlus1').val('false');
        $('#gapDayOfStartAndCompletePlus2').val('false');
        $('#gapDayOfStartAndCompletePlus3').val('false');
        $('#gapDayOfStartAndCompletePlus4').val('true');
        $('#gapDayOfStartAndCompletePlus5').val('false');

        $('#searchCondition').submit();
    });

    $('#gapDayOfStartAndCompletePlus5-btn').click(function() {
        $('#gapDayOfStartAndCompletePlus1').val('false');
        $('#gapDayOfStartAndCompletePlus2').val('false');
        $('#gapDayOfStartAndCompletePlus3').val('false');
        $('#gapDayOfStartAndCompletePlus4').val('false');
        $('#gapDayOfStartAndCompletePlus5').val('true');

        $('#searchCondition').submit();
    });


    $('#gapDayOfOrderAndCompletePlus2-btn').click(function() {
        $('#gapDayOfOrderAndCompletePlus2').val('true');
        $('#gapDayOfOrderAndCompletePlus3').val('false');
        $('#gapDayOfOrderAndCompletePlus4').val('false');
        $('#gapDayOfOrderAndCompletePlus5').val('false');
        $('#gapDayOfOrderAndCompletePlus6').val('false');

        $('#searchCondition').submit();
    });

    $('#gapDayOfOrderAndCompletePlus3-btn').click(function() {
        $('#gapDayOfOrderAndCompletePlus2').val('false');
        $('#gapDayOfOrderAndCompletePlus3').val('true');
        $('#gapDayOfOrderAndCompletePlus4').val('false');
        $('#gapDayOfOrderAndCompletePlus5').val('false');
        $('#gapDayOfOrderAndCompletePlus6').val('false');

        $('#searchCondition').submit();
    });

    $('#gapDayOfOrderAndCompletePlus4-btn').click(function() {
        $('#gapDayOfOrderAndCompletePlus2').val('false');
        $('#gapDayOfOrderAndCompletePlus3').val('false');
        $('#gapDayOfOrderAndCompletePlus4').val('true');
        $('#gapDayOfOrderAndCompletePlus5').val('false');
        $('#gapDayOfOrderAndCompletePlus6').val('false');

        $('#searchCondition').submit();
    });

    $('#gapDayOfOrderAndCompletePlus5-btn').click(function() {
        $('#gapDayOfOrderAndCompletePlus2').val('false');
        $('#gapDayOfOrderAndCompletePlus3').val('false');
        $('#gapDayOfOrderAndCompletePlus4').val('false');
        $('#gapDayOfOrderAndCompletePlus5').val('true');
        $('#gapDayOfOrderAndCompletePlus6').val('false');

        $('#searchCondition').submit();
    });

    $('#gapDayOfOrderAndCompletePlus6-btn').click(function() {
        $('#gapDayOfOrderAndCompletePlus2').val('false');
        $('#gapDayOfOrderAndCompletePlus3').val('false');
        $('#gapDayOfOrderAndCompletePlus4').val('false');
        $('#gapDayOfOrderAndCompletePlus5').val('false');
        $('#gapDayOfOrderAndCompletePlus6').val('true');

        $('#searchCondition').submit();
    });


    $('#firstPage').click(function() {
        $('#page').val(1);
        $('#searchCondition').submit();
    });

    $('#prevPage').click(function() {
        $('#page').val($(this).attr('data-page'));
        $('#searchCondition').submit();
    });

    $('#nextPage').click(function() {
        $('#page').val($(this).attr('data-page'));
        $('#searchCondition').submit();
    });

    $('#lastPage').click(function() {
        $('#page').val($(this).attr('data-page'));
        $('#searchCondition').submit();
    });

    $('.clickPage').click(function() {
        $('#page').val($(this).attr('data-page'));
        $('#searchCondition').submit();
    });


    $('#form-submit').click(function(){
        $('#page').val(1);
        $('#searchCondition').submit();
    });


    jQuery.download = function(url, data, method) {
        //url and data options required
        if (url && data) {
            //data can be string of parameters or array/object
            data = typeof data == 'string' ? data : jQuery.param(data);
            //split params into form inputs
            var inputs = '';
            jQuery.each(data.split('&'), function () {
                var pair = this.split('=');
                inputs += '<input type="hidden" name="' + pair[0] +
                    '" value="' + pair[1] + '" />';
            });
            //send request
            jQuery('<form action="' + url +
                '" method="' + (method || 'post') + '">' + inputs + '</form>')
                .appendTo('body').submit().remove();
        }
    };

    $('#form-excel').click(function() {
        var action = $('#searchCondition').attr('action');
        if(action.indexOf('fileDown') == -1)
            action += '/fileDown';

        $.download(action, $('#searchCondition').serialize(), 'GET');
    });

	// Horizontal Slider
	$('#h-slider').slider({
		range: true,
		values: [17, 67]
	});
	$('#h-slider-2').slider({
		range: true,
		values: [4, 75]
	});
	$('#h-slider-3').slider({
		range: true,
		values: [45, 97]
	});
	$('#h-slider-4').slider({
		range: true,
		values: [25, 37]
	});
	$('#h-slider-5').slider({
		range: true,
		values: [3, 27]
	});

	// Vertical sliders
	$( "#eq > span" ).each(function() {
		// read initial values from markup and remove that
		var value = parseInt( $( this ).text(), 10 );
		$( this ).empty().slider({
			value: value,
			range: "min",
			animate: true,
			orientation: "vertical"
		});
	});

    // Autocomplete
    var availableTags = ["ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme"];
     
    $("#tags").autocomplete({
    	source: availableTags
    });

    // Menu
    $("#menu").menu();

    // Accordion
    $( "#accordion" ).accordion({
        header: '.widget-title',
        animation: "easeInOutBounce",
        collapsible: true,
        heightStyle: "content"
    });

    $('.col-grid').sortable({
    	connectWith: '.col-grid',
    	placeholder: "ui-state-highlight"
    });

    // Spinner
	var spinner = $( "#spinner" ).spinner();
	 
	$( "#disable" ).click(function() {
		if ( spinner.spinner( "option", "disabled" ) ) {
			spinner.spinner( "enable" );
		} else {
			spinner.spinner( "disable" );
		}
	});
	$( "#destroy" ).click(function() {
		if ( spinner.data( "ui-spinner" ) ) {
			spinner.spinner( "destroy" );
		} else {
			spinner.spinner();
		}
	});
	$( "#getvalue" ).click(function() {
		alert( spinner.spinner( "value" ) );
	});
	$( "#setvalue" ).click(function() {
		spinner.spinner( "value", 5 );
	});
});